package com.jinlia.show.modules.openservice.config;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;

@Configuration
public class ConfigBean {
    @Value("${aliyun.access-key-id}")
    public String aliyunAccessKeyId;//文件访问路径

    @Value("${aliyun.access-key-secret}")
    public String aliyunAccessKeySecret;//文件访问路径
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean(name = "asyncPromiseExecutor")
    public ScheduledExecutorService asyncPromiseExecutor() {

        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("THREAD_NAME_WITH_SCHEDULE").build();
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2,
                threadFactory);
        return scheduledExecutorService;
    }

    @Bean
    public AsyncClient buildAsyncClient() {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(aliyunAccessKeyId)
                .accessKeySecret(aliyunAccessKeySecret)
                .build());
        AsyncClient asyncClient = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .build();
        return asyncClient;
    }
}
